﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;
using DAT = CPPEI.ETL.Domain.Data;

namespace CPPEI.ETL.WebApi.COM
{
    [CPPEI.WebApi.AuthGetAction("COM", "GetLogList", "1.0")]
    public class GetLogListAction : BaseAction<GetLogListParams, GetLogListResult>
    {
        protected override ActionResult<GetLogListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetLogListParams opParams)
        {
            SER.LogService service = new SER.LogService();
            PagedList<ENT.Log> result = service.GetPagedList(opParams.Page * 15, 15);
            if(result.IsSuccess)
            {
                GetLogListResult actResult = new GetLogListResult();
                actResult.Page = opParams.Page;
                actResult.Start = result.Start;
                actResult.Limit = result.Limit;
                actResult.Total = result.Total;
                for (int index = 0; index < result.Data.Count; index++)
                {
                    GetLogListResult.Item taskCount = new GetLogListResult.Item();
                    taskCount.Id = result.Data[index].Id;
                    taskCount.Level = result.Data[index].Level;
                    taskCount.Message = result.Data[index].Message;
                    taskCount.Tag = result.Data[index].Tag;
                    taskCount.CreatedTime = result.Data[index].CreatedTime;
                    actResult.Items.Add(taskCount);
                }

                return new ActionResult<GetLogListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetLogListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class GetLogListParams
    {
        public int Page { get; set; }
    }

    public class GetLogListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }
            public int Level { get; set; }
            public string Message { get; set; }
            public string Tag { get; set; }
            public DateTime CreatedTime { get; set; }
        }
    }
}
